package com.fitso.model.dao.hibernate.type.measure;

import java.math.BigDecimal;

import com.fitso.model.bean.measure.Measure;
import com.fitso.model.bean.measure.energy.Energy;

/**
 * Conversion utility to/from the database/java type of {@link Energy} types
 * 
 * @author timothystorm
 */
public abstract class EnergyHibernateType extends MeasureHibernateType {

	protected BigDecimal convertToBigDecimal(Measure measure) {
		return (measure == null || !measure.isInitialized()) ? null : getInstance().convert((Energy) measure)
				.getValue();
	}

	protected Measure convertToMeasure(BigDecimal value) {
		if (value == null) {
			return null;
		}
		Energy energy = getInstance();
		energy.setValue(value);
		return energy;
	}

	protected Measure copy(Measure measure) {
		return measure == null || !measure.isInitialized() ? null : getInstance().convert((Energy) measure);
	}

	public Class<Energy> returnedClass() {
		return Energy.class;
	}

	abstract protected Energy getInstance();
}
